package com.ymatou.query.seller.trading.api.parameter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.utils.DataUtil;

/**
 * 存储过程：sp_trd_GetOrdersByOrderIds_v2
 * 
 * @author songyefeng
 *
 */
@SuppressWarnings("rawtypes")
public class Sp_GetOrdersByOrderIds_v2{
	private List<Integer> orders;
	private Integer seller = null;

	public void setOrders(List<Integer> orders) {
		this.orders = orders;
	}

	public void setSeller(Integer seller) {
		this.seller = seller;
	}

	public Sp_GetOrdersByOrderIds_v2 execute() {
		if (DataUtil.isNullOrEmpty(orders) == false) {
			queryOrderList();
			queryOrderInfoList();
			queryOrderStateList();
		}
		return this;
	}

	private void queryOrderList() {
		StringBuffer a = new StringBuffer();
		// Basic
		a.append("o.iOrderId as Basic_ID");
		a.append(",o.dAddTime as Basic_CreatedTime");
		a.append(",o.dCancelTime as Basic_CancelledTime");
		a.append(",o.dAcceptTime as Basic_AcceptedTime");
		a.append(",o.iTradingStatus as Basic_Status");
		a.append(",o.iRiskVerifiedStatus as Basic_RiskStatus");
		a.append(",o.bShangouOrder as Basic_Type");
		a.append(",e.sOrderSource as Basic_Source");
		a.append(",IIF(o.bShangouOrder=1,o.fTotalPrice,o.fOrderPrice) as Basic_TotalPrice");
		a.append(",o.fOrderDiscount as Basic_Discount");
		// Buyer
		a.append(",o.iUserId as Buyer_ID");
		a.append(",o.sBuyerLoginId as Buyer_LoginId");
		a.append(",o.sBuyerLoginEmail as Buyer_Email");
//		a.append(",o.sBuyerNickName as Buyer_NickName");
		a.append(",u.sNickName as Buyer_NickName");
		a.append(",o.sLeaveWord as Buyer_Message");
		// Seller
		a.append(",o.iBuyerId as Seller_ID");
		a.append(",o.sSellerLoginId as Seller_LoginId");
		a.append(",o.sSellerLoginEmail as Seller_Email");
		a.append(",(select top 1 n.sContent from Ymt_O_OrderNote(nolock) n where n.iOrderId = o.iOrderId and n.iUserId = o.iBuyerId) as Seller_Comment");
		// Consignee
		a.append(",o.sAddress as Consignee_Address");
		a.append(",o.sReceivePerson as Consignee_Name");
		a.append(",o.sPhone as Consignee_Phone");
		a.append(",o.sTelephone as Consignee_Tel");
		a.append(",o.sPostCode as Consignee_Postcode");
		// Payment
		a.append(",(");
//		a.append("	s.fPaidAmountOfCash + s.fPostPaidAmountOfCash - o.fFreight");
		a.append("	s.fPaidAmountOfCash + s.fPostPaidAmountOfCash");
		a.append(") as Payment_ActualPaid");
		a.append("	,o.dPaidTime as Payment_1_PaidTime");
		a.append(",o.iTradingId as Payment_1_InternalTradingNo");
		a.append("	,(select top 1 sTradeNo from Ymt_TradingInfo(nolock) t where s.fPaidAmountOfCash > 0 and t.iTradingId = o.iTradingId and t.iTradingStatus = 2) as Payment_1_ExternalTradingNo");
		a.append("	,o.dPostPaidTime as Payment_2_PaidTime");
		// Transfer
		a.append(",o.dDispathTime as Transfer_DeliveredTime");
		a.append(",o.dConfirmedTime as Transfer_ReceiptConfirmedTime");

		StringBuffer b = new StringBuffer();
		b.append(" left join Ymt_OrderExt(nolock) e on o.iOrderId = e.iOrderId");
		b.append(" left join Ymt_OrderState(nolock) s on o.iOrderId = s.iOrderId");
		b.append(" left join ymt_users(nolock) u on o.iUserId = u.iUserId");

		StringBuffer c = new StringBuffer("o.iOrderId in "
				+ DataUtil.toInOfSql(orders));
		if (seller != null) {
			c.append(" and o.iBuyerId = " + seller);
		}
		orderList = new YmtOrdersWapper().selectOrders2(a.toString(),
				b.toString(), c.toString());
	}

	private void queryOrderInfoList() {
		StringBuffer a = new StringBuffer();
		a.append(" oi.iOrderId as OrderId");
		a.append(",oi.sTitle as [Name]");
		a.append(",oi.sCatalogId as CatalogId");
		a.append(",oi.sProductId as ProductId");
		a.append(",oi.fProductPrice as Price");
		a.append(",oi.fProductOriginalPrice as OriginalPrice");
		a.append(",oi.iAmount as [Count]");
		a.append(",oi.fOriginalPrice * oi.iAmount as TotalPrice");
		a.append(",oi.iCatalogType as CatalogType");
		a.append(",oi.sSKU as SKU");
		a.append(",oi.sPropertyInfo as PropertyInfo");
		a.append(",e.iActivityId as ActivityId");
		a.append(",oi.sPackageNo as PackageNo");
		a.append(",oi.iBondedArea as BondedArea");
		a.append(",oi.iCatalogStatus as Logistics");
		a.append(",oi.iProductMainCategoryId as Category1");
		a.append(",oi.iProductSubCategoryId as Category2");
		a.append(",oi.iProductThirdCategoryId as Category3");
		a.append(",oi.fFreight as PartialFreight");
		a.append(",oi.fYmtCouponAmount as PartialYmtCoupon");
		a.append(",refund.*");
		a.append(",oi.iProductBrandId as BrandId");
		a.append(",oi.fDiscount AS PartialDiscount");
		a.append(",oi.fSellerCouponAmount AS PartialSellerCoupon");
		a.append(",oi.fSellerPromotionAmount AS PartialSellerPromotion");

		StringBuffer b = new StringBuffer();
		b.append(" left join Ymt_OrderInfoExt e with(nolock,ForceSeek)");
		b.append(" on oi.sOrderInfoId = e.sOrderInfoId");
		b.append(" outer apply (");
		b.append(" 	select top 1");
		b.append(" 		[t1].[RefundAmount], [t1].[RefundedAmountOfCash], [t1].[RefundedAmountOfGift], [t1].[RefundStatus], ");
		b.append(" 		[t1].[SettlementAmountOfCoupon], [t1].[SalesRefundStatus], [t2].[RefundProductNum]");
		b.append(" 	from [dbo].[Ymt_RefundBill] as t1 with(nolock)");
		b.append(" 	inner join [dbo].[Ymt_RefundProduct] as t2 with(nolock)");
		b.append(" 	on t1.[RefundBillNo] = t2.[RefundBillNo] and [t2].[OrderId] = [t1].[OrderId] and [t1].[OrderId] = oi.[iOrderId] and [t2].[OrderInfoId] = oi.[sOrderInfoId] and [t1].[SalesRefundStatus] = 10");
		b.append(" ) as refund");
		orderInfoList = new DefaultWapper().selectOrderInfo(a.toString(),
				b.toString(), " oi.iOrderId in " + DataUtil.toInOfSql(orders));
	}

	private void queryOrderStateList() {
		StringBuffer a = new StringBuffer();
		a.append(" o.iOrderId as OrderId");
		a.append(" ,state.fPaidAmountOfCash as PaidCash");
		a.append(" ,state.fPaidAmountOfGift as PaidGift");
		a.append(" ,state.fPaidAmountOfFreeCard as PaidFreeCard");
		a.append(" ,state.fPaidAmountOfYmtCoupon as PaidYmtCoupon");
		a.append(" ,state.fPaidAmountOfSellerCoupon as PaidSellerCoupon");
		a.append(" ,state.fPostPaidAmountOfCash as PostPaidCash");
		a.append(" ,state.fPostPaidAmountOfGift as PostPaidGift");
		a.append(" ,o.fTotalPrice as TotalProductPrice");
		a.append(" ,o.fOrderPrice as FirstPayProductPrice");
		a.append(" ,o.fFreight as Freight");
		a.append(" ,o.fOrderDiscount as OrderDiscount");
		a.append(" ,o.fDiscount as PostPayDiscount");
		a.append(" ,o.fSellerPromotionAmount as TotalActivityPrice");
//		a.append(" ,r.TotalRefundedCash");
//		a.append(" ,r.TotalRefundedGift ");
//		a.append(" ,r.TotalRefundedYmtCoupon");
		a.append(" ,state.fRefundedAmountOfCash as TotalRefundedCash");
		a.append(" ,state.fRefundedAmountOfGift as TotalRefundedGift");
		a.append(" ,state.fRefundedAmountOfCoupon as TotalRefundedYmtCoupon");

		StringBuffer b = new StringBuffer();
		b.append(" join Ymt_Orders(nolock) o");
		b.append(" on state.iorderid = o.iorderid");
//		b.append(" outer apply (");
//		b.append(" 	select sum(RefundedAmountOfCash) as TotalRefundedCash, sum(RefundedAmountOfGift) as TotalRefundedGift, sum(SettlementAmountOfCoupon) as TotalRefundedYmtCoupon");
//		b.append(" 	from Ymt_RefundBill(nolock) where OrderId = o.iOrderId and SalesRefundStatus = 10 and RefundStatus = 1");
//		b.append(" select sum(os.fRefundedAmountOfCash) as TotalRefundedCash, sum(os.fRefundedAmountOfGift) as TotalRefundedGift, sum(os.fRefundedAmountOfCoupon) as TotalRefundedYmtCoupon");
//		b.append(" from Ymt_OrderState os where os.iOrderId=o.i");
//		b.append(" ) r");

		orderStateList = new DefaultWapper().selectOrderStatesByOrderIds(
				a.toString(), b.toString(),
				"o.iOrderId in " + DataUtil.toInOfSql(orders));
	}

	private List<Map> orderList = new ArrayList<Map>();
	private List<Map> orderInfoList = new ArrayList<Map>();
	private List<Map> orderStateList = new ArrayList<Map>();

	/**
	 * 订单商品详情列表
	 */

	public List<Map> getOrderList() {
		return orderList;
	}

	public List<Map> getOrderInfoList() {
		return orderInfoList;
	}

	public List<Map> getOrderStateList() {
		return orderStateList;
	}

}
